<template>
    <div class="reportFormPage" v-if="show">
        <div id="domid" class="report-dom">
            <!-- 头部标题区 -->
            <el-row>
                <el-col :span="5" :offset="19">
                    <div class="pageNumBox">
                        <span>第</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
                        <span>页，共</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
                        <span>页</span>
                    </div>
                </el-col>
            </el-row>
            <div class="topTwoBox">
                <div class="centerName">{{ form.baobmc }}</div>
                <div class="codeText">{{ form.baobbh }}</div>
            </div>
            <div class="threeTopBox">
                <div class="left-border">
                    <div class="box-title">检测单位名称：</div>
                    <div class>
                        {{
                        form.jiancdwmc
                        }}
                    </div>
                </div>
                <div class="right-border">
                    <span>记录编号：</span>
                    <span class="placeholder">{{ form.jilbh }}</span>
                </div>
            </div>
            <!-- 表格主要内容区 -->
            <div class="table-border">
                <!-- 主要内容表格 -->
                <table height="220px" class="top-table">
                    <tbody>
                    <tr>
                        <td height="30" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">工程名称</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcmc }}</span>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">工程部位/用途</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcbwyt }}</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" align="center" width="15%">样品信息</td>
                        <td class="specimen-border" colspan="3">
                            <span>样品名称：</span>
                            <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                            <span>；样品编号：</span>
                            <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                            <span>；样品数量：</span>
                            <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                            <span>；样品状态：</span>
                            <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                            <span>；来样时间：</span>
                            <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
                        </td>
                    </tr>

                    <tr>
                        <td height="25" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">试验检测日期</span>
                        </td>
                         <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">试验条件</span>
                        </td>
                        <td v-if="isPdf" width="35%">
                            温度：
                            <span style="display: inline-block; width: 10%">{{ form.shiytjwd }}</span>
                            ℃；湿度：
                            <span style="display: inline-block; width: 10%">{{ form.shiytjsd }}</span
                            >%RH
                        </td>
                        <td v-else width="35%">
                            温度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" style="width: 15%"/>
                            ℃；湿度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" style="width: 15%"/>
                            %RH
                        </td>
                    </tr>

                    <tr>
                        <td height="50" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">检测依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.jiancyj }}</span>
                            <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark"
                                      @dblclick.native="showBsJiancyjView" readonly/>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">判定依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.pandyj }}</span>
                            <el-input v-else type="textarea" v-model="form.pandyj" :rows="2"
                                      @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')"
                                      class="mark"/>
                        </td>
                    </tr>
                    <tr class="bottom-border">
                        <td height="50" align="center" width="15%">
                            <div>主要仪器设备</div>
                            <div>名称及编号</div>
                        </td>
                        <td colspan="3">
                            <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                            <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh"
                                      class="height100 width100 mark" @dblclick.native="showBsInstrumentView"
                                      readonly></el-input>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 表1 -->
                <table class="middle-table" style="height: 90px;">
                    <tbody>
                    <tr>
                        <td class="text-center" style="width: 15%;" height="30">
                            <span style="width:90px;display:inline-block">取样地点</span>
                        </td>
                        <td class="text-center" colspan="3">
                            <el-input type="textarea"
                                      v-model="form.quydd"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 0, 0)"
                                      :ref="'0_0'"
                                      :readonly="isPdf"
                            />
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" style="width: 15%;" height="30">
                            <span style="width:90px;display:inline-block">代表数量</span>
                        </td>
                        <td class="text-center" style="width: 35%;">
                            <el-input type="textarea"
                                      v-model="form.daibsl"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 1, 0)"
                                      :ref="'1_0'"
                                      :readonly="isPdf"
                            />
                        </td>
                        <td class="text-center" style="width: 15%;">
                            <span style="width:90px;display:inline-block">批号</span>
                        </td>
                        <td class="text-center" style="width: 35%;">
                            <el-input type="textarea"
                                      v-model="form.pih"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 1, 1)"
                                      :ref="'1_1'"
                                      :readonly="isPdf"
                            />
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" style="width: 15%;">
                            <span style="width:90px;display:inline-block">出厂日期</span>
                        </td>
                        <td class="text-center" style="width: 35%;">
                            <el-date-picker class="hide-icon"
                                            v-model="form.chucrq"
                                            value-format="yyyy/MM/dd"
                                            format="yyyy-MM-dd"
                                            style="width: 100%"
                                            @change="backBgzFun"
                                            :readonly="isPdf"/>
                        </td>
                        <td class="text-center" style="width: 15%;">
                            <span style="width:90px;display:inline-block">生成厂家</span>
                        </td>
                        <td class="text-center" style="width: 35%;">
                            <el-input type="textarea"
                                      v-model="form.cd"
                                      @change="backBgzFun"
                                      @keydown.native="handleKeyDown($event, 2, 0)"
                                      :ref="'2_0'"
                                      :readonly="isPdf"
                            />
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 表2-主要内容 -->
                <table class="middle-table" style="height: 180px;">
                    <tbody>
                    <tr>
                        <td class="text-center" style="width: 6%;" height="300" rowspan="6">
                            <span style="width:20px;display:inline-block">密度</span>
                        </td>
                        <td class="text-center" style="width: 16%;" height="50">
                            <span style="width:90px;display:inline-block">试验次数</span>
                        </td>
                        <td class="text-center" style="width: 20%;">
                            <span style="width:90px;display:inline-block">比重瓶质量(g)</span>
                        </td>
                        <td class="text-center" style="width: 20%;">
                            <span style="width:90px;display:inline-block">比重瓶盛满水时质量(g)</span>
                        </td>
                        <td class="text-center" style="width: 18%;">
                            <span style="width:100px;display:inline-block">比重瓶与沥青试样质量(g)</span>
                        </td>
                        <td class="text-center" style="width: 20%;" colspan="2">
                            <span style="width:120px;display:inline-block">比重瓶、试样及水质量(g)</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50">
                            <el-input type="number"
                                      v-model="form.a1"
                                      @keydown.native="handleKeyDown($event, 3, 0)"
                                      @paste.native="handlePaste($event,  0, 0)"
                                      :ref="`3_0`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.b1"
                                      @change="computeXdmd(1)"
                                      @keydown.native="handleKeyDown($event, 3, 1)"
                                      @paste.native="handlePaste($event,  0, 1)"
                                      :ref="`3_1`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.c1"
                                      @change="computeXdmd(1)"
                                      @keydown.native="handleKeyDown($event, 3, 2)"
                                      @paste.native="handlePaste($event,  0, 2)"
                                      :ref="`3_2`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.d1"
                                      @change="computeXdmd(1)"
                                      @keydown.native="handleKeyDown($event, 3, 3)"
                                      @paste.native="handlePaste($event,  0, 3)"
                                      :ref="`3_3`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td colspan="2">
                            <el-input type="number"
                                      v-model="form.e1"
                                      @change="computeXdmd(1)"
                                      @keydown.native="handleKeyDown($event, 3, 4)"
                                      @paste.native="handlePaste($event,  0, 4)"
                                      :ref="`3_4`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td height="50">
                            <el-input type="number"
                                      v-model="form.a2"
                                      @keydown.native="handleKeyDown($event, 4, 0)"
                                      @paste.native="handlePaste($event,  1, 0)"
                                      :ref="`4_0`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.b2"
                                      @change="computeXdmd(2)"
                                      @keydown.native="handleKeyDown($event, 4, 1)"
                                      @paste.native="handlePaste($event,  1, 1)"
                                      :ref="`4_1`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.c2"
                                      @change="computeXdmd(2)"
                                      @keydown.native="handleKeyDown($event, 4, 2)"
                                      @paste.native="handlePaste($event,  1, 2)"
                                      :ref="`4_2`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td>
                            <el-input type="number"
                                      v-model="form.d2"
                                      @change="computeXdmd(2)"
                                      @keydown.native="handleKeyDown($event, 4, 3)"
                                      @paste.native="handlePaste($event,  1, 3)"
                                      :ref="`4_3`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                        <td colspan="2">
                            <el-input type="number"
                                      v-model="form.e2"
                                      @change="computeXdmd(2)"
                                      @keydown.native="handleKeyDown($event, 4, 4)"
                                      @paste.native="handlePaste($event,  1, 4)"
                                      :ref="`4_4`"
                                      :readonly="isPdf"
                            ></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-center" height="50">
                            <span style="width:90px;display:inline-block">试验温度(℃)</span>
                        </td>
                        <td class="text-center">
                            <span style="width:90px;display:inline-block">水密度(g/cm³)</span>
                        </td>
                        <td class="text-center" colspan="2">
                            <span style="width:90px;display:inline-block">相对密度</span>
                        </td>
                        <td class="text-center" colspan="2">
                            <span style="width:90px;display:inline-block">密度(g/cm²)</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="100" rowspan="2">
                            <el-select v-model="form.a3" class="hide-icon height100" placeholder
                                       @change="shuiwenChange(1)">
                                <el-option v-for="(item, index) in shuiwenList" :key="index" :label="item.temp"
                                           :value="item.temp"></el-option>
                            </el-select>
                        </td>
                        <td rowspan="2">
                            <span>{{ form.b3 }}</span>
                        </td>
                        <td height="50">
                            <span>{{ form.c31 }}</span>
                        </td>
                        <td rowspan="2">
                            <span>{{ form.d3 }}</span>
                        </td>
                        <td>
                            <span>{{ form.e31 }}</span>
                        </td>
                        <td rowspan="2" style="width: 10%;">
                            <span>{{ form.f3 }}</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50">
                            <span>{{ form.c32 }}</span>
                        </td>
                        <td>
                            <span>{{ form.e32 }}</span>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 附加声明 -->
                <table height="120px" class="bottom-table">
                    <tr>
                        <td>
                            <el-input type="textarea" :rows="2" v-model="form.fujsm" :readonly="isPdf"/>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- 表格底部签名区域 -->
            <table height="22px" class="transparent">
                <tbody>
                <tr>
                    <td width="10%" align="right">检测：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">记录：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">复核：</td>
                    <td align="left" width="15%"></td>
                    <td width="10%">日期：</td>
                    <td align="left" width="15%">
                        <div style="display: inline-block; margin-left: 20px">年</div>
                        <div style="display: inline-block; margin-left: 20px">月</div>
                        <div style="display: inline-block ;margin-left: 20px">日</div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
        <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
        <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
    </div>
</template>

<script>
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";
import {handleFocus, handleKeyDown, handlePaste} from "@/views/reportForms/utils";
import decimal from "@/utils/big-decimal";
import {getYsDzkyqdsyBsInfo} from "@/api/reportys/ysYsdzkyqdsyjcjlbBsInfo";
import {listBySyTypeId} from "@/api/sycs/dataBase";
import Bus from "@/utils/bus";
import {addLqMdsyjcBsInfo, getLqMdsyjcBsInfo, updateLqMdsyjcBsInfo} from "@/api/report_lq/mdsyjc";

export default {
    name: "mdsyjc",
    components: {BsJudgeBaseView, BsInstrumentView, BsJiancyjView},
    props: {
        excelId: {
            type: [String, Number],
            default: () => ""
        },
        syType: {
            type: [String, Number],
            default: () => ""
        },
        page: {
            type: Number,
            default: () => 0
        },
        isPdf: {
            type: Boolean,
            default: () => false
        },
        printEmpty: {
            type: Boolean,
            default: () => false
        }
    },
    data() {
        return {
            show: false,
            form: {
                excelId: undefined,
                totalPage: undefined,
                page: undefined,
                jilbh: undefined,
                jiancdwmc: undefined,
                gongcmc: undefined,
                gongcbwyt: undefined,
                yangpmc: undefined,
                yangpbh: undefined,
                yangpsl: undefined,
                yangpzt: undefined,
                yangpsj: undefined,
                shiyjcrq: undefined,
                shiytjwd: undefined,
                shiytjsd: undefined,
                jiancyj: undefined,
                pandyj: undefined,
                jiancyjIds: [],
                pandyjIds: [],
                zhuyyqsbmcjbh: undefined,
                shebIds: [],
                fujsm: undefined,

                quydd: undefined,
                cd: undefined,
                daibsl: undefined,
                pih: undefined,
                chucrq: undefined,

                a1: undefined,
                b1: undefined,
                c1: undefined,
                d1: undefined,
                e1: undefined,
                a2: undefined,
                b2: undefined,
                c2: undefined,
                d2: undefined,
                e2: undefined,
                a3: undefined,
                b3: undefined,
                c31: undefined,
                c32: undefined,
                d3: undefined,
                e31: undefined,
                e32: undefined,
                f3: undefined,
            },
            shuiwenList: []
        };
    },
    created() {
        this.init();
        this.getShuiwenList();
    },
    methods: {
        init() {
            this.show = false;
            getLqMdsyjcBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
                this.form = response.data;
                if (!this.form.fujsm) {
                    this.form.fujsm = "附加声明：";
                }
                this.setInitialValue();
                this.show = true;
            });
        },
        // 设置初始值
        setInitialValue() {
            if (this.form) {
                this.form.baobmc = this.form.baobmc || "沥青密度试验检测记录表";
                this.form.baobbh = this.form.baobbh || "JGLQ10001";
                this.form.a1 = this.form.a1 || 1
                this.form.a2 = this.form.a2 || 2
                if (!this.form.a1) {
                    this.$set(this.form, 'a1', 1)
                }
                if (!this.form.a2) {
                    this.$set(this.form, 'a2', 2)
                }
            }
        },

        // 获取水温水密度数据
        getShuiwenList() {
            listBySyTypeId({syYjId: "281757153833"}).then((response) => {
                const data = response.data;
                this.shuiwenList = data.page.records;
            });
        },

        // 水温改变
        shuiwenChange(index) {
            const info = this.shuiwenList.find((item) => item.temp == this.form.a3);
            if (!info) {
                return;
            }
            this.form.b3 = info.density;
            for (let i = 1; i <= 2; i++) {
                this.computeMd(i)
            }
        },

        // 显示仪器列表
        showBsInstrumentView() {
            this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
        },
        // 仪器选择
        selectInstrument(info) {
            this.form.zhuyyqsbmcjbh = info.label;
            this.form.shebIds = info.ids;
            // Bus.$emit("setYsJcBgZhuyyqsbmcjbh", {zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds});
        },
        // 显示检测依据列表
        showBsJiancyjView() {
            this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
        },
        // 检测依据选择
        selectJiancyj(info) {
            this.form.jiancyj = info.label;
            this.form.jiancyjIds = info.ids;
            // Bus.$emit("setYsJcBgJiancyj", {jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds});
        },
        // 显示判定依据列表
        showBsJudgeBaseView() {
            this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
        },
        // 判断依据选择
        selectJudge(info) {
            if (!this.form.pandyj) {
                this.form.pandyj = info.label;
                return;
            }
            if (info && info.label) {
                this.form.pandyj += "、" + info.label;
            }
        },
        handleFocus(event) {
            handleFocus(event, this.page, this.form[event]);
        },

        // 计算相对密度
        computeXdmd(index) {
            let molecule = decimal.subtract(this.form['d' + index], this.form['b' + index], 3)
            let denominator = decimal.subtract(decimal.subtract(this.form['c' + index], this.form['b' + index], 3),
                decimal.subtract(this.form['e' + index], this.form['d' + index], 3), 3)

            this.form['c3' + index] = denominator ? decimal.divide(molecule, denominator, 3) : 0

            // 计算平均相对密度
            this.computePjXdmd()
            // 计算密度
            this.computeMd(index)
        },

        // 计算平均相对密度
        computePjXdmd() {
            this.form.d3 = decimal.divide(decimal.add(this.form.c31, this.form.c32, 3), 2, 3)
        },

        // 计算密度
        computeMd(index) {
            this.form['e3' + index] = decimal.multiply(this.form.b3, this.form['c3' + index], 3)
            // 计算平均密度
            this.computePjMd()
        },

        // 计算平均密度
        computePjMd() {
            let md = decimal.divide(decimal.add(this.form.e31, this.form.e32, 3), 2, 3)
            this.form.f3 = md
            Bus.$emit("setLqJCBgPjMd", {
                md: md,
            });
        },

        // 返回报告引入值
        backBgzFun() {
            // Bus.$emit("setYsBgBase", {
            //     shiyyt: this.form.shiyyt,
            //     yansclms: this.form.yansclms,
            //     shijxz: this.form.shijxz,
            // });
        },

        // 提交表单
        submitForm() {
            return this.form;
        },

        // 鼠标移动操作
        handleKeyDown(event, rowIndex, colIndex) {
            const maxArr = [0, 1, 0, 4, 4, 5, 1]; // 每一行最多有多少可聚焦输入框（从0开始）
            const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
            if (this.$refs[ref]?.length) {
                this.$refs[ref][0].focus();
            } else if (this.$refs[ref]) {
                this.$refs[ref].focus();
            }
        },
        // 粘贴赋值方法-密度试验次数
        handlePaste(event, rowIndex, colIndex) {
            const nameAllArr = [
                ["a1", "b1", "c1", "d1", "e1"],
                ["a2", "b2", "c2", "d2", "e2"],
            ];
            const subscriptArr = [
                [0, 1, 2, 3, 4],
                [0, 1, 2, 3, 4],
            ];

            const methodsAllArr = [
                [null, 'computeXdmd', 'computeXdmd', 'computeXdmd', 'computeXdmd'],
                [null, 'computeXdmd', 'computeXdmd', 'computeXdmd', 'computeXdmd'],
            ];
            const parameterArr = [
                [null, 1, 1, 1, 1],
                [null, 2, 2, 2, 2],
            ];
            const data = handlePaste(event, rowIndex, colIndex, nameAllArr, methodsAllArr, parameterArr, subscriptArr);
            const {cols, nameSetArr, methodsSetArr, parameterSetArr, subscriptSetArr} = data;
            for (let a = 0; a < nameSetArr.length; a++) {
                for (let b = 0; b < nameSetArr[a].length; b++) {
                    this.form[nameSetArr[a][b]] = cols[a][b];
                }
            }
            for (let a = 0; a < methodsSetArr.length; a++) {
                for (let b = 0; b < methodsSetArr[a].length; b++) {
                    if (methodsSetArr[a][b]) {
                        this.setCalculate(methodsSetArr[a][b], parameterSetArr[a][b]);
                    }
                }
            }
        },

        // 计算方法调用
        setCalculate(name, a, b, c, d, e) {
            this[name](a, b, c, d, e);
        }
    }
}
</script>

<style scoped lang="scss">
.td-input {
  width: 100% !important;
  height: 30px !important;
}

</style>
